package com.tianya.scheduled.task;

import com.alibaba.fastjson.JSON;
import com.tianya.scheduled.constant.CronConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.http.HttpEntity;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 调用Http定时任务
 *
 * ClassName: HttpCallTask <br/>
 * Description: <br/>
 * date: 2021/5/23 12:54<br/>
 *
 * @author tiany<br />
 */
@Component
@Slf4j
public class HttpCallTask {

    @Autowired
    private RestTemplate restTemplate;

    @Scheduled(cron = CronConstant.PER_20_SECONDS)
    public void printCron() throws JSONException {
        JSONObject json = new JSONObject();
        json.put("pageSize",10);
        json.put("pageNum", 1);

//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//        headers.add("x-auth-token", "AAA");
        HttpEntity entity = new HttpEntity(JSON.toJSONString(json));
        String s = restTemplate.postForObject("http://localhost:8010/cron-expression/query", entity, String.class);
        System.out.println(s);
    }

    @Scheduled(cron = CronConstant.PER_5_SECONDS)
    public void callSayHello(){
        String s = restTemplate.getForObject("http://localhost:8081/test/sayHello", String.class);
        System.out.println(s);
    }
}
